<?php
//本文件做的事情： 爬若干页，图片保存到本地并产生新文件名，信息入库zfw.zfw_articles    文章内容还没采集
//要求php永不超时 只能用终端 不能用nginx apache，他们会超时
set_time_limit(0);

//绝对路径
include __DIR__ . '/function.php';
require __DIR__ . '/vendor/autoload.php';

use QL\QueryList;

//目标网页的格式:
//https://news.ke.com/bj/baike/0033/pg1/
//https://news.ke.com/bj/baike/0033/pg2/
//...
//https://news.ke.com/bj/baike/0033/pg7/


$rang = range(1,3);

//采集
//对于图片的话，进行额外处理，先下载下来
foreach ($rang as $page) {
    $url = 'https://news.ke.com/bj/baike/0033/pg' . $page. '/';
    //echo $url;

    $html = http_request($url);
    //分析采集
    $datalist = QueryList::Query($html, [ //这里头key名和数据库的字段保持了一致(偷懒)
        "pic" => ['.lj-lazy', 'data-original','', function($item) {
            //$item是 图片的url
            //得到扩展名
            $ext = pathinfo($item, PATHINFO_EXTENSION);
            //生成唯一文件名
            $filename = 'beike_' . md5($item). '_' . time(). '.' . $ext;
            //生成的本地路径
            $filepath = dirname(__DIR__) . '/public/uploads/article/' . $filename;
            //把$item的内容请求到，重命名到本地$filepath
            file_put_contents($filepath, http_request($item));
            //把存到数据库的pic字段内容返回， 可以在$datalist看到
            return '/uploads/article/' . $filename;
        }],
        "title" => ['.item .text .LOGCLICK', 'text'],
        "desn" => ['.item .text .summary', 'text'],
        "url" => ['.item .text > a', 'href']
    ])->data;
}

//入库
$db = new PDO('mysql:host=localhost;dbname=zfw;charset=utf8mb4','root', 'root');

foreach ($datalist as $val) {
    //牛逼访问$val元素方法：
    extract($val);// 超牛逼解构 可以直接访问$title, $pic $desn $url
    $sql = "insert into zfw_articles (title,desn,pic,url,body) values('$title','$desn','$pic','$url','')";
    $stmt = $db->prepare($sql);
    $stmt->execute();

    //传统访问$val元素方法：
    //$sql = "insert into zfw_articles (title,desn,pic,url,body) values(?,?,?,?,'')";
   // $stmt = $db->prepare($sql);
    //入库
   // $stmt->execute([$val['title'], $val['desn'], $val['pic'], $val['url']]);
}

var_dump($datalist);
